/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * tasas_servicio.java
 *
 * Created on 9/01/2011, 01:01:20 PM
 */
package interfaz;

import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author jorge
 */
public class tasas_servicio extends javax.swing.JDialog {

    /** Creates new form tasas_servicio */
    int num;
    JLabel[] nombre, velocidad;
    JTextField[] txtNombre, txtVelocidad;
    ArrayList<logica.datos_servicio> todos;

    public tasas_servicio(java.awt.Frame parent, boolean modal, int num, ArrayList todos) {
        super(parent, modal);
        this.num = num;
        this.todos = todos;
        initComponents();
        this.setIconImage(new ImageIcon(getClass().getResource("/images/face-monkey.png")).getImage());

        nombre = new JLabel[num];
        velocidad = new JLabel[num];
        txtNombre = new JTextField[num];
        txtVelocidad = new JTextField[num];
        for (int i = 0; i < num; i++) {
            nombre[i] = new JLabel("Nombre servidor " + Integer.toString(i + 1));
            nombre[i].setBounds(0, 20 * i, 150, 20);
            velocidad[i] = new JLabel("Tasa de servicio");
            velocidad[i].setBounds(300, 20 * i, 100, 20);
            txtNombre[i] = new JTextField("Nombre servidor_" + Integer.toString(i + 1));
            txtNombre[i].setBounds(150, 20 * i, 150, 20);
            txtVelocidad[i] = new JTextField();
            txtVelocidad[i].setBounds(400, 20 * i, 100, 20);
            jPanel1.add(nombre[i]);
            jPanel1.add(txtNombre[i]);
            jPanel1.add(velocidad[i]);
            jPanel1.add(txtVelocidad[i]);

        }
    }

    private boolean validar() {
        try {
            for (int i = 0; i < num; i++) {
                if (txtNombre[i].getText().equals("")) {
                    return false;
                }
                if (txtVelocidad[i].getText().equals("")) {
                    return false;
                }
            }
        } catch (Exception ev) {
            return false;
        }
        return true;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jScrollPane1.setAutoscrolls(true);

        jPanel1.setLayout(null);
        jScrollPane1.setViewportView(jPanel1);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/emblem-default.png"))); // NOI18N
        jButton1.setText("Aceptar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/edit-delete.png"))); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Unidad");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Clientes/hora", "Clientes/min", "Clientes/seg" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2)
                .addGap(34, 34, 34)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 272, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(23, 23, 23))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 663, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        /*        switch (jComboBox2.getSelectedIndex()) {
        case 1:
        tasa_servicio = Double.parseDouble(jtservicio.getText()) / conver.minToseg(1);
        break;
        case 2:
        tasa_servicio = Double.parseDouble(jtservicio.getText());
        break;
        case 0:
        tasa_servicio = Double.parseDouble(jtservicio.getText()) / conver.hrToseg(1);
        break;
        }
         */

        double tmp, velocidad = 0;
        logica.convertir conver = new logica.convertir();

        if (validar()) {
            for (int i = 0; i < num; i++) {
                tmp = Double.parseDouble(txtVelocidad[i].getText());
                switch (jComboBox2.getSelectedIndex()) {
                    case 1:
                        velocidad = tmp / conver.minToseg(1);
                        break;
                    case 2:
                        velocidad = tmp;
                        break;
                    case 0:
                        velocidad = tmp / conver.hrToseg(1);
                        break;
                }
                todos.add(new logica.datos_servicio(txtNombre[i].getText(), velocidad));
            }
            dispose();
        } else {
            javax.swing.JOptionPane.showMessageDialog(null, "Error, los datos son incorrectos", "Error", javax.swing.JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
